package com.qingfeng.manager.controller;

import com.github.pagehelper.PageInfo;
import com.qingfeng.core.data.ResultData;
import com.qingfeng.core.entity.Role;
import com.qingfeng.manager.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description: 角色控制
 * @author:LWY
 * @date: 2017/10/27 17:49
 * @modified:
 */
@RestController
@RequestMapping("/role")
public class RoleController {


    @Autowired
    RoleService roleService;

    /**
     * 获取所有的角色
     */
    @RequestMapping("/")
    public ResultData get(HttpServletRequest request) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isEmpty(pageNum) || StringUtils.isEmpty(pageSize)) {
            pageNum = "0";
            pageSize = "0";
        }
        String role = request.getParameter("role");
        String state = request.getParameter("state");
        PageInfo<Role> all = roleService.get(Integer.valueOf(pageNum), Integer.valueOf(pageSize), role, state);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "成功", all);
        return resultData;
    }


    /**
     * 获取所有角色并且标记某用户有哪些角色
     *
     * @param
     * @return
     */
    @RequestMapping("/userinfo/{userinfo_id}")
    public ResultData forUserinfo(@PathVariable String userinfo_id) {
        List<Map> all = roleService.forUserinfo(userinfo_id);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "成功", all);
        return resultData;
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping("/del")
    public ResultData del(HttpServletRequest request) {
        String[] ids = request.getParameterValues("ids[]");
        roleService.del(ids);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "删除成功", null);
        return resultData;
    }

    /**
     * 保存
     *
     * @return
     */
    @RequestMapping("/save")
    public ResultData save(HttpServletRequest request) {
        String id = request.getParameter("id");
        String role = request.getParameter("role");
        String description = request.getParameter("description");
        Role r = roleService.save(id, role, description);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "保存成功", r);
        return resultData;
    }


    /**
     * 保存授权
     *
     * @return
     */
    @RequestMapping("/savePermission")
    public ResultData savePermission(HttpServletRequest request) {
        String role_id = request.getParameter("role_id"); //角色id
        String[] permission_ids = request.getParameterValues("permission_ids[]");//权限id数组
        roleService.savePermission(role_id, permission_ids);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "保存成功", "");
        return resultData;
    }

}
